const events = require('./events');

class Fortune {
  constructor(date) {
    this.date = date || new Date();
    this.seed = this.getDateSeed();
  }

  // 根据日期生成种子
  getDateSeed() {
    const year = this.date.getFullYear();
    const month = this.date.getMonth() + 1;
    const day = this.date.getDate();
    return year * 10000 + month * 100 + day;
  }

  // 根据种子获取随机数
  random(max, seed) {
    const s = seed || this.seed;
    return Math.abs((s * 9301 + 49297) % 233280) % max;
  }

  // 获取今日运势
  getTodayFortune() {
    const goodCount = 2 + this.random(3);
    const badCount = 2 + this.random(3);

    const goods = this.pickRandomItems(events.good, goodCount);
    const bads = this.pickRandomItems(events.bad, badCount);
    const direction = events.directions[this.random(events.directions.length)];
    const drink = this.pickRandomItems(events.drinks, 2);
    const stars = 1 + this.random(5);

    return {
      goods,
      bads,
      direction: `面向${direction}写程序，BUG最少`,
      drinks: drink.join('配'),
      stars
    };
  }

  // 随机选择指定数量的项目
  pickRandomItems(array, count) {
    const result = [];
    const tempArray = [...array];
    
    for (let i = 0; i < count && tempArray.length > 0; i++) {
      const index = this.random(tempArray.length, this.seed + i);
      result.push(tempArray.splice(index, 1)[0]);
    }
    
    return result;
  }
}

module.exports = Fortune; 